﻿using CardMonitor.Model;
using CardMonitor.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CardMonitor.Windows
{
    /// <summary>
    /// MakeCardWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MakeCardWindow : Window
    {
        SynchronizationContext _syncContext = null;
        public BillModel billModel;

        public MakeCardWindow()
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;

            billModel = new BillModel();

            this.Loaded += Winndow_Loaded;
        }

        public void setModel(BillModel billModel)
        {
            this.billModel = billModel;
        }
        private void Winndow_Loaded(object sender, RoutedEventArgs e)
        {
            new Thread(() =>
            {
                //String uuid = CardUtil.getCardGUID();
               // String code = CardUtil.getCardGUIDCode(billModel.FacId);
                String code = CardUtil.getCardGUIDCode(billModel.DriverCard);
                //App.getGlobalVariables().Guid = uuid;
                //billModel.MaterialCode
/*                string prodCode = billModel.ProdCode;
                if (prodCode == null) prodCode = "00";

                prodCode = billModel.BusinessCode;*/
                //prodCode = billModel.MaterialName;
/*                if (prodCode.Length > 2)
                {
                    prodCode = prodCode.Substring(0, 2);
                }*/
                //App.getGlobalVariables().prodCode = prodCode;
               // string mCode = billModel.MaterialCode;//物料编码
               // String carNumCode = "";
                //if (billModel.ProductAttrValueId.Contains("oil"))
                //{
                //    string density = App.getConfig().density;
                //    double midu = double.Parse(density);
                //    string yznum= Math.Round((billModel.Num * midu), 0).ToString();
                //    carNumCode = CardUtil.getCarNumCode(billModel.CarNumber, prodCode, yznum);
                //}
                //else
                //{
                  //  carNumCode = CardUtil.getCarNumCode(billModel.CarNumber,  (billModel.Nnum * 1000).ToString());

                //}
               // String icCode = CardUtil.getIcCode("0000");
                
                //String billNo = billModel.Code.Substring(billModel.Code.Length - 8);
                
               //// String billCode = CardUtil.getBillCode(billNo);
                //App.getGlobalVariables().billCode = billNo;

                //写卡
                try
                {
                    if (!App.getGlobalVariables().cardComm.PortState)
                    {
                        App.getGlobalVariables().cardComm.OpenPort(App.getConfig().IC_Card_PortName, 9600, 8, 1);
                    }
                    Thread.Sleep(100);
                    App.getGlobalVariables().cardComm.SendHexData(code);
                    if (App.getGlobalVariables().cardComm.PortState)
                    {
                        App.getGlobalVariables().cardComm.ClosePort();
                    }
                    Thread.Sleep(500);
                }
                catch (Exception ex)
                {
                    _syncContext.Post(CloseWindow, "error");
                    return;
                }
                Thread.Sleep(500);
                
                //吐卡
                //App.getGlobalVariables().cardDeviceComm.SendHexData("F2 00 00 03 43 32 30 03 B3");
                
                /*
                String ioPort = App.getConfig().IO_Com_Port;

                byte _port = Convert.ToByte(int.Parse(ioPort));

                byte[] byte01 = new byte[10];
                byte01[0] = 0x48;
                byte01[1] = 0x3A;
                byte01[2] = 0x01;
                byte01[3] = 0x70;
                byte01[4] = _port;
                byte01[5] = 0x01;
                byte01[6] = 0x00;
                byte01[7] = 0x00;
                byte01[8] = 0x45;
                byte01[9] = 0x44;

                App.getGlobalVariables().ioComm.SendByteData(byte01);

                Thread.Sleep(500);

                byte01[5] = 0x00;
                App.getGlobalVariables().ioComm.SendByteData(byte01);

                Thread.Sleep(500);
                */

                //billModel.Juid = uuid;

                _syncContext.Post(CloseWindow, "success");
            }).Start();
        }
        private void CloseWindow(object text)
        {
            string status = (string)text;
            if ("success".Equals(status))
            {
                this.DialogResult = true;
            }
            this.Close();
        }
    }
}
